package com.yzf.demo.demo.Thread.ThreadPool;

import java.util.concurrent.*;

/**
 * Created by 于占峰 on 2020/4/4/004.
 * 实现可监控的线程池
 */
public class ExecutorsSelf {

    /**
     * 创建固定大小的线程池 自定义ThreadPoolSelf
     *
     * @param nThreads
     * @return
     */
    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolSelf(nThreads, nThreads,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>());
    }

    /**
     * 创建自动扩展的线程池 自定义ThreadPoolSelf
     *
     * @return
     */
    public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolSelf(0, Integer.MAX_VALUE,
                60L, TimeUnit.SECONDS,
                new SynchronousQueue<Runnable>());
    }

    /**
     * 创建只有一个任务的线程池 自定义ThreadPoolSelf
     *
     * @return
     */
    public static ExecutorService newSingleThreadExecutor() {
        return new ThreadPoolSelf.FinalizableDelegatedExecutorService
                (new ThreadPoolExecutor(1, 1,
                        0L, TimeUnit.MILLISECONDS,
                        new LinkedBlockingQueue<Runnable>()));
    }

    /**
     * 创建带有定时任务的线程池 自定义ScheduledThreadPoolExecutorSelf 实现befor after
     *
     * @param corePoolSize
     * @return
     */
    public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutorSelf(corePoolSize);
    }


}
